#!/usr/bin/env bash
# Ref:
# https://www.reddit.com/r/KittyTerminal/comments/132n4p5/any_lf_file_manager_users_how_to_preview_pdfs_as/
# https://github.com/rafi/.config/blob/master/lf/preview

# NOTE:
# For lf,
# `exit 1` means lf will not cache the preview result, and will run the cleaner script. Kitty need to exit 1 to solve the picture still remains after preview.
# `exit 0` means lf will cache the preview result and will acclerate the next time preview for same file. If not previewed by kitty, we can exit 0.

FILEPATH="$(realpath "$1")"
W="$2"
H="$(((${3:-0} * 99) / 100))" # 99% of the height
X="$4"
Y="$5"

FILENAME="$(basename "$FILEPATH")"
FILENAMEWITHOUTEXT="${FILENAME%.*}"
FILEEXT="${FILENAME##*.}"
# FILE_EXTENSION="${FILE##*.}"
# FILE_EXTENSION_LOWER="$(printf "%s" "${FILE_EXTENSION}" | tr '[:upper:]' '[:lower:]')"
FILETYPE="$(file --dereference --brief --mime-type -- "$FILEPATH")"

# _TMPFILENAME="$(basename "$FILE" | tr ' ' '__' | tr '.' '_')".png
TMPFILEDIR=/tmp/lf-img"$(dirname "$FILEPATH")"
TMPFILENAMEWITHOUTEXT="$(sha256sum "$FILEPATH" | awk '{print $1}')"
TMPFILENAME="$TMPFILENAMEWITHOUTEXT".png
TMPFILEPATH="${TMPFILEDIR}/${TMPFILENAME}"
# mkdir -p "$TMPFILEDIR"

function image_previewer() {
    local experimental # Non-empty is true, default leave empty for false.
    experimental="$2"

    if [[ "$TERM" == 'xterm-kitty' || (-n "$TMUX" && -n "$experimental") ]]; then
        local transfer_mode # Only when ssh, use stream mode. If local, use file mode.
        transfer_mode="$([ -n "$SSH_TTY" ] && echo 'stream' || echo 'file')"

        kitty +kitten icat --silent --stdin no --transfer-mode "$transfer_mode" --place "${W}x${H}@${X}x${Y}" "$1" </dev/null >/dev/tty && exit 1
    fi

    # fallback
    local chafa_mode=symbols

    # For alacritty, I set alacritty default $TERM to xterm-256color
    # But in tmux, tmux will change the $TERM to screen-256color, this is also I specifically made it.
    # However, if one day in the future, I set tmux $TERM to xterm-256color, it will cause the picture to be displayed incorrectly.
    # So now I have to add the condition of -z "$TMUX" to prevent things happening in the future, even if this is an accidental case.
    if [[ ("$TERM" == 'xterm-256color' && -z "$TMUX") || "$TERM" == 'st-256color' || "$TERM" == 'wezterm' ]]; then
        chafa_mode=sixels
    fi

    # chafa -f sixels -s "$W"x"$H" "$1" --animate off && exit 0
    # chafa -f sixels -s "$W"x"$H" "$1" --polite on --animate off && exit 0
    # chafa -f sixels -s "$W"x"$H" "$1" --polite on --animate off --colors 240 && exit 0
    # chafa -f "$chafa_mode" -s "$W"x"$H" --polite on --animate off --colors 240 "$1" && exit 0

    # Ref: https://github.com/gokcehan/lf/wiki/Previews#with-sixel
    chafa -f "$chafa_mode" -s "$W"x"$H" --animate off --polite on -t 1 --colors 240 "$1" && exit 0

    # fallback if no chafa
    cat "$1"
    exit 0
}

function handle_extension() {
    case "$FILEPATH" in
    *.nii | *.nii.gz)
        # git clone git@git.zhlh6.cn:ysl2/niicat.git ~/.vocal/niicat
        if [ ! -f "$TMPFILEPATH" ]; then
            mkdir -p "$TMPFILEDIR"
            niicat "$FILEPATH" "$TMPFILEPATH"
        fi
        # image_previewer "$TMPFILEPATH" experimental
        image_previewer "$TMPFILEPATH"
        ;;
    *.six | *.sixel)
        if [ ! -f "$TMPFILEPATH" ]; then
            mkdir -p "$TMPFILEDIR"
            sixel2png "$FILEPATH" >"$TMPFILEPATH"
        fi
        image_previewer "$TMPFILEPATH"
        ;;
    *.pkl)
        python -c "import pickle; from pprint import pprint; p = pickle.load(open('$FILEPATH', 'rb')); pprint(p)"
        ;;
    *.doc | *.docx | *.ppt | *.pptx | *.xls | *.xlsx)
        if [ ! -f "$TMPFILEPATH" ]; then
            mkdir -p "$TMPFILEDIR"
            if [[ "$FILEEXT" == "doc" || "$FILEEXT" == "docx" ]]; then
                tmp=writer_pdf_Export
            elif [[ "$FILEEXT" == "ppt" || "$FILEEXT" == "pptx" ]]; then
                tmp=impress_pdf_Export
            elif [[ "$FILEEXT" == "xls" || "$FILEEXT" == "xlsx" ]]; then
                tmp=calc_pdf_Export
            fi
            # Ref: https://help.libreoffice.org/latest/en-US/text/shared/guide/pdf_params.html
            libreoffice --headless --convert-to "pdf:${tmp}:{\"PageRange\":{\"type\":\"string\",\"value\":\"1\"},\"Quality\":{\"type\":\"long\",\"value\":\"25\"},\"MaxImageResolution\":{\"type\":\"long\",\"value\":\"75\"}}" --outdir "$TMPFILEDIR" "$FILEPATH" >/dev/null
            mv "${TMPFILEDIR}/${FILENAMEWITHOUTEXT}".pdf "${TMPFILEDIR}/${TMPFILENAMEWITHOUTEXT}.pdf"
            pdftoppm -f 1 -l 1 -png -r 72 -aa no -aaVector no "${TMPFILEDIR}/${TMPFILENAMEWITHOUTEXT}.pdf" >"$TMPFILEPATH"
        fi
        image_previewer "$TMPFILEPATH"
        ;;
    *.json | *.tgn)
        python -m json.tool "$FILEPATH"
        ;;
    *.HEIC | *.xcf)
        if [ ! -f "$TMPFILEPATH" ]; then
            mkdir -p "$TMPFILEDIR"
            # sips -s format png "$FILE" --out "$TMPFILEPATH" >/dev/null
            # NOTE: this `[0]` below is inteneded to handle multi-frame images. Not a bug.
            magick "$FILEPATH[0]" -strip -colors 240 -quality 0 -define png:compression-level=0 -define png:compression-strategy=0 "$TMPFILEPATH"
        fi
        image_previewer "$TMPFILEPATH"
        ;;
    *.drawio)
        if [ ! -f "$TMPFILEPATH" ]; then
            mkdir -p "$TMPFILEDIR"
            drawio "$FILEPATH" --no-sandbox -x -f png -s 0.75 -o "$TMPFILEPATH" >/dev/null
        fi
        image_previewer "$TMPFILEPATH"
        ;;
    esac
    # WARNING: Do not add any exit value here!
}

function handle_mime() {
    case "$FILETYPE" in
    image/*)
        image_previewer "$FILEPATH"
        ;;
    *pdf)
        if [ ! -f "$TMPFILEPATH" ]; then
            mkdir -p "$TMPFILEDIR"
            pdftoppm -f 1 -l 1 -png -r 72 -aa no -aaVector no "$FILEPATH" >"$TMPFILEPATH"
        fi
        image_previewer "$TMPFILEPATH"
        ;;
    audio/*)
        if [ ! -f "$TMPFILEPATH" ]; then
            mkdir -p "$TMPFILEDIR"
            ffmpeg -i "$FILEPATH" -filter_complex "showwavespic=s=640x480" -frames:v 1 "$TMPFILEPATH"
        fi
        image_previewer "$TMPFILEPATH"
        ;;
    video/*)
        if [ ! -f "$TMPFILEPATH" ]; then
            mkdir -p "$TMPFILEDIR"
            # sudo apt install -y ffmpeg
            # ffmpeg -i "$FILEPATH" -vf "thumbnail" -frames:v 1 "$TMPFILEPATH"
            ffmpeg -ss 00:00:00 -i "$FILEPATH" -frames:v 1 "$TMPFILEPATH"
        fi
        image_previewer "$TMPFILEPATH"
        ;;
    *zip | *tar | *rar | *x-xz)
        # sudo apt install atool
        # als "$FILE" | awk '{print $NF}'
        # brew install unar
        lsar "$FILEPATH"
        exit 0
        ;;
    *)
        cat "$FILEPATH"
        exit 0
        ;;
    esac
    # WARNING: Do not add any exit value here!
}

function main() {
    handle_extension
    handle_mime
}

main "$@"
